// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import App from './App'
import {store} from './store/store'

import Home from './components/Home.vue'
import HelloWorld from './components/HelloWorld.vue'
import Translate from './components/Translate.vue'
import ProductList from './components/ProductList.vue'
import Login from './components/Login.vue'

Vue.config.productionTip = false
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(ElementUI)

// 配置路由
const router = new VueRouter({
  routes:[
    {path:"/", component:Home, name:"Root"},
    {path:"/helloworld", component:HelloWorld, name:"Helloworld"},
    {path:"/translate", component:Translate, name:'Translate'},
    {path:"/productlist", component:ProductList,name:'ProductList'},
    {path:"/login", component:Login, name:'Login'}
  ],
  mode:"history"
})

router.beforeEach((to, from, next)=> {
  if (to.name == 'Login') {
    next();
    return;
  }
  var name = store.state.user.name;
  if (name == '未登录') {
    if (to.meta.requireAuth || to.name == null) {
      next({path: '/', query: {redirect: to.path}})
    } else {
      next();
    }
  } else {
    //initMenu(router, store);
    //if(to.path=='/home')
    //  store.commit("updateMsgList", []);
    next();
  }
}
)

/* eslint-disable no-new */
new Vue({
  store:store,
  router,
  el: '#app',
  components: { App },
  template: '<App/>'
})
